package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.PurchaseOderandinvoiceMapper;
import cn.tedu.erp.project.pojo.dto.PurchaseOderandinvoiceUpdateDTO;
import cn.tedu.erp.project.pojo.dto.PurchaseOderandinvoiceAddNewDTO;
import cn.tedu.erp.project.pojo.entity.PurchaseOderandinvoice;
import cn.tedu.erp.project.pojo.vo.PurchaseOderandinvoiceListVO;
import cn.tedu.erp.project.pojo.vo.PurchaseOderandinvoiceStandardVO;
import cn.tedu.erp.project.service.IPurchaseOderandinvoiceService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PurchaseOderandinvoiceServiceImpl implements IPurchaseOderandinvoiceService {
    @Autowired
    PurchaseOderandinvoiceMapper mapper;
    @Override
    public void addNew(PurchaseOderandinvoiceAddNewDTO purchaseOderandinvoiceAddNewDTO) {
        log.debug("开始处理【添加发票】的业务，参数：{}", purchaseOderandinvoiceAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的发票的名称
        String name = purchaseOderandinvoiceAddNewDTO.getCompanyName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = mapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "添加发票失败，发票名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建PurchaseOderandinvoice对象
        PurchaseOderandinvoice purchaseOderandinvoice = new PurchaseOderandinvoice();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到PurchaseOderandinvoice对象中
        BeanUtils.copyProperties(purchaseOderandinvoiceAddNewDTO, purchaseOderandinvoice);
        // 调用Mapper对象的insert()执行插入发票数据
        log.debug("即将执行插入数据，参数：{}", purchaseOderandinvoice);
        mapper.insert(purchaseOderandinvoice);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除发票】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        PurchaseOderandinvoiceStandardVO purchaseOderandinvoice = mapper.getStandardById(id);
        log.debug("根据ID={}检查发票数据是否存在，查询结果：{}", id, purchaseOderandinvoice);
        // 判断查询结果是否为null
        if (purchaseOderandinvoice == null) {
            // 是：数据不存在，抛出异常
            String message = "删除发票失败，尝试删除的发票数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        mapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, PurchaseOderandinvoiceUpdateDTO purchaseOderandinvoiceUpdateDTO) {
        log.debug("开始处理【修改发票详情】的业务，ID：{}，新数据：{}", id, purchaseOderandinvoiceUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        PurchaseOderandinvoiceStandardVO queryResult = mapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改发票详情失败，尝试修改的发票数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的countByNameAndNotId()执行统计
        int count = mapper.countByNameAndNotId(id, purchaseOderandinvoiceUpdateDTO.getCompanyName());
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "修改发票详情失败，发票名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建PurchaseOderandinvoice对象
        PurchaseOderandinvoice purchaseOderandinvoice = new PurchaseOderandinvoice();
        // 复制属性，设置ID
        BeanUtils.copyProperties(purchaseOderandinvoiceUpdateDTO, purchaseOderandinvoice);
        purchaseOderandinvoice.setId(id);
        // 调用Mapper对象的update()方法执行修改
        mapper.update(purchaseOderandinvoice);
    }

    @Override
    public PurchaseOderandinvoiceStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询发票详情】的业务，参数：{}", id);
        // PurchaseOderandinvoiceStandardVO queryResult = mapper.getStandardById(id);
        PurchaseOderandinvoiceStandardVO queryResult = mapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询发票详情失败，发票数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public void getStandardBycommodityCode(Long commodityCode) {
        log.debug("开始处理【根据商品编号查询发票详情】的业务，参数：{}", commodityCode);
        int count=mapper.countByCommodityCode(commodityCode);
        if(count>0){
            String message = "添加发票失败，发票已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //PurchaseOderandinvoiceStandardVO queryResult = mapper.getStandardByCommodityCode(commodityCode);
//        if (!(queryResult == null)) {
//            // 是：抛出异常
//            String message = "查询发票详情失败，发票已存在！";
//            log.warn(message);
//            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
//        }

    }

    @Override
    public List<PurchaseOderandinvoiceListVO> list() {
        log.debug("开始处理【查询发票列表】的业务，无参数");
        // List<PurchaseOderandinvoiceListItemVO> list = mapper.list();
        List<PurchaseOderandinvoiceListVO> list = mapper.list();
        return list;
    }

    @Override
    public void rebuildCache() {

    }
}
